package com.cn.aam.checaiduo.ui.me;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.minterface.MyOnPageChangeListener;
import com.cn.aam.checaiduo.ui.homepage.adapter.MyFragmentPageAdapter;
import com.cn.aam.checaiduo.ui.me.fragment.InvestRecordFragment;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 投资记录
 */
public class InvestRecordActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.tab_layout)
    SegmentTabLayout tabLayout;
    @BindView(R.id.tab_viewPager)
    ViewPager tabViewPager;
    private List<Fragment> fragments;

    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, InvestRecordActivity.class);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_invest_record;
    }

    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent)
                .setNavigationIcon(R.drawable.ic_nav_arrow_white)
                .setBackgroundColor(R.color.colorPrimary)
                .setRightMenuIconRes(R.drawable.ic_choose)
                .setRightMenuIconClickListener(this)
                .setTitle("投资记录");

        //tabLayout
        String[] titles = {"募集中", "起息中", "已回款"};
        fragments = new ArrayList<>();
        fragments.add(InvestRecordFragment.newInstance(InvestRecordFragment.INVEST_COLLECT));
        fragments.add(InvestRecordFragment.newInstance(InvestRecordFragment.INVEST_ONSALE));
        fragments.add(InvestRecordFragment.newInstance(InvestRecordFragment.INVEST_PAYMENT));

        tabViewPager.setAdapter(new MyFragmentPageAdapter(getSupportFragmentManager(), fragments));

        tabLayout.setTabData(titles);

        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                tabViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        tabViewPager.addOnPageChangeListener(new MyOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (MyViewsUtils.isFastDoubleClick()) return;
        startActivityForResult(ChooseDateActivity.newInstance(mContext), 0);
        overridePendingTransition(R.anim.slide_in_from_bottom,-1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            String dateStart = data.getStringExtra("dateStart");
            String dateEnd = data.getStringExtra("dateEnd");
            int position = tabLayout.getCurrentTab();
            ((InvestRecordFragment) fragments.get(position)).setDataAndNotify(dateStart, dateEnd);
        }
    }
}
